package com.ruoyi.common.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class NumberGeneratorUntil {

    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    public static RedisTemplate<String, Integer> redis;

    @PostConstruct
    public void getRedisTemplate() {
        redis = this.redisTemplate;
    }


    /**
     * 生成审批单号
     *
     * @param prefix
     * @return
     */
    public String generateApprovalFormCode(String prefix) {
        Integer approvalFormNumber = NumberGeneratorUntil.redis.opsForValue().get("approvalFormNumber");
        if (ObjectUtils.isEmpty(approvalFormNumber)) {
            approvalFormNumber = 1;
        }
        String formattedDate = generateFormattedDate(); // 生成当前日期
        String result = prefix + "-" + formattedDate + "-" + String.format("%04d", approvalFormNumber);
        NumberGeneratorUntil.redis.opsForValue().set("approvalFormNumber", ++approvalFormNumber);
        return result;
    }


    /**
     * 发布管理 申请单号
     *
     * @param prefix
     * @return
     */
    public String generateRlsManageFormCode(String prefix) {
        Integer approvalFormNumber = NumberGeneratorUntil.redis.opsForValue().get("RlsManageFormCode");
        if (ObjectUtils.isEmpty(approvalFormNumber)) {
            approvalFormNumber = 1;
        }
        String formattedDate = generateFormattedDate(); // 生成当前日期
        String result = prefix + "-" + formattedDate + "-" + String.format("%04d", approvalFormNumber);
        NumberGeneratorUntil.redis.opsForValue().set("RlsManageFormCode", ++approvalFormNumber);
        return result;
    }

    /**
     * 通知模版/审批流编号 id
     *
     * @param prefix
     * @return
     */
    public String generateRlsManageFlowCode(String prefix) {
        Integer approvalFormNumber = NumberGeneratorUntil.redis.opsForValue().get("RlsManageFlowCode");
        if (ObjectUtils.isEmpty(approvalFormNumber)) {
            approvalFormNumber = 1;
        }
        String result = prefix + "-" + String.format("%04d", approvalFormNumber);
        NumberGeneratorUntil.redis.opsForValue().set("RlsManageFlowCode", ++approvalFormNumber);
        return result;
    }

    /**
     * 通知模版/审批流编号 id
     *
     * @param prefix
     * @return
     */
    public String generateRlsManageNotifyCode(String prefix) {
        Integer approvalFormNumber = NumberGeneratorUntil.redis.opsForValue().get("RlsManageNotifyCode");
        if (ObjectUtils.isEmpty(approvalFormNumber)) {
            approvalFormNumber = 1;
        }
        String result = prefix + "-" + String.format("%04d", approvalFormNumber);
        NumberGeneratorUntil.redis.opsForValue().set("RlsManageNotifyCode", ++approvalFormNumber);
        return result;
    }

    /**
     * 生成日常巡检编号
     *
     * @param prefix
     * @return
     */
    public String generateRoutineInspectionNextNumber(String prefix) {
        Integer riCurrentNumber = NumberGeneratorUntil.redis.opsForValue().get("riCurrentNumber");
        if (ObjectUtils.isEmpty(riCurrentNumber)) {
            riCurrentNumber = 1;
        }
        String formattedDate = generateFormattedDate(); // 生成当前日期
        String result = prefix + "-" + formattedDate + "-" + String.format("%04d", riCurrentNumber);
        NumberGeneratorUntil.redis.opsForValue().set("riCurrentNumber", ++riCurrentNumber);
        return result;
    }

    /**
     * 生成工单编号
     *
     * @param prefix
     * @return
     */
    public String generateWorkOrderNextNumber(String prefix) {
        Integer woCurrentNumber = NumberGeneratorUntil.redis.opsForValue().get("woCurrentNumber");
        if (ObjectUtils.isEmpty(woCurrentNumber)) {
            woCurrentNumber = 1;
        }
        String formattedDate = generateFormattedDate(); // 生成当前日期
        String result = prefix + "-" + formattedDate + "-" + String.format("%04d", woCurrentNumber);
        NumberGeneratorUntil.redis.opsForValue().set("woCurrentNumber", ++woCurrentNumber);
        return result;
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    private static String generateFormattedDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            NumberGeneratorUntil until = new NumberGeneratorUntil();
            String riCurrentNumber = until.generateRoutineInspectionNextNumber("CK");
            System.out.println("日常巡检编码：" + riCurrentNumber);
        }
        for (int i = 0; i < 10; i++) {
            NumberGeneratorUntil until = new NumberGeneratorUntil();
            String woCurrentNumber = until.generateRoutineInspectionNextNumber("TKI");
            System.out.println("工单编码：" + woCurrentNumber);
        }

    }
}